import {
  Controller,
  Get,
  Post,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { MinioService } from './minio.service';
import { FileInterceptor } from '@nestjs/platform-express';

@Controller('minio')
export class MinioController {
  constructor(private readonly minioService: MinioService) {}

  @Get('upload-test')
  async uploadTest() {
    return await this.minioService.test();
  }

  @Get('test')
  async test() {
    return await this.minioService.test();
  }

  @Get('presignedUrl')
  async presignedUrl(@Query('name') name: string) {
    return this.minioService.presignedUrl(name);
  }

  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  async uploadFile(@UploadedFile() file: Express.Multer.File) {
    const bucketName = 'xf-bucket';
    const objectName = file.originalname;
    const fileBuffer = file.buffer;
    const fileSize = file.size;
    const metaData = {
      'Content-Type': file.mimetype,
    };
    console.log(objectName);
    console.log(file);
    try {
      const url = await this.minioService.uploadFile(
        bucketName,
        objectName,
        fileBuffer,
        fileSize,
        metaData,
      );
      return { url };
    } catch (error) {
      return { message: 'Upload failed', error: error.message };
    }
  }
}
